var app = getApp();
//引入这个插件，使html内容自动转换成wxml内容
var WxParse = require('../../wxParse/wxParse.js');
Page({
    data:{
        indicatorDots:true,
        autoplay: true,
        interval: 5000,
        duration: 1000,
        goodsId:'',
        goodsInfo: {},
        commentInfo:[],
        host: app.d.hostUrl,
        currentTab: 0, //tab切换
        buynum: 1,
    },

    //初始化商品数据
    onLoad:function(options){
        var that = this;
        that.setData({
            goodsId : options.goodsId,
        });
        that.loadGoodsDetail();
    },

    //请求商品详情接口
    loadGoodsDetail:function(){
        var that = this;
        wx.request({
            url: app.d.hostUrl + '/api/goods-detail',
            method : 'get',
            data : {
                goodsId : that.data.goodsId
            },
            header: {
                'Content-Type':  'application/x-www-form-urlencoded'
            },
            success:function(res){
                var status = res.data.status;
                var data = res.data.info;
                if(status == 0){
                    wx.showToast({
                        title: data,
                        duration: 2000
                      });
                    return false;
                };
                WxParse.wxParse('content', 'html', data.detail, that, 3);
                that.setData({
                    goodsInfo: data,
                    commentInfo: data.comments,
                });

            },
            error:function(e){
                wx.showToast({
                    title: '网络异常！',
                    duration: 2000
                  });
                return false;
            },
        });
    },

    //商品详情切换
    swichNav:function(e){
        var that = this;
        if(that.data.currentTab === e.target.dataset.current){
            return false;
        }else{
            that.setData({
                currentTab : e.target.dataset.current
            });
        }
    },

    //滑动切换tab
    bindChange:function(e){
        var that = this;
        console.log(e.detail.current);
        that.setData({
            currentTab : e.detail.current
        });
    },

    //添加购物车
    addShopCart:function(res){
        var that = this;
        wx.request({
            url: app.d.hostUrl + '/api/cart-add',
            method: 'get',
            data: {
                user_id: app.d.userId,
                goods_id: that.data.goodsId,
                num: that.data.buynum,
            },
            header: {
                'Content-Type':  'application/x-www-form-urlencoded'
            },

            success:function(res){
                var status = res.data.status;
                var data = res.data.info;
                if(status == 0){
                    wx.showToast({
                        title: data,
                        duration: 2000
                    });
                    return false;
                }else{
                    wx.showToast({
                        title: data,
                        duration: 2000
                    });
                }
            },
            error:function(e){
                wx.showToast({
                    title: '网络错误！',
                    duration: 2000
                });
                return false;
            },

        });
    },
});